<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class cateController extends Controller
{
    public static function getCatePid($pid)
    {
        $data = DB::table('shop_cate') -> where('pid', $pid) -> get();
        $arr = array();
        foreach($data as $k => $v)
        {
            $v['sub'] = self::getCatePid($v['id']);
            $arr[] = $v;
        }
        return $arr;
    }
    /**
<<<<<<< HEAD
    *表单验证名称是否存在
=======
    *表单验证
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    */
    public function getVerify(Request $request)
    {
        $cname = $request -> input('cname');
        $res = DB::table('shop_cate') -> where('cname',$cname) -> first();
        if($res)
            echo 1;
    }
    /**
    *打包类
    */
    public static function getCate()
    {
        $data = DB::table('shop_cate') ->select(DB::raw('*,concat(path,",",id) as paths')) -> orderBy('paths') -> get();
        foreach($data as $k => $v){
            $l = substr_count($v['path'],',');
            $data[$k]['cname'] = str_repeat('|----',$l).$v['cname'];
        }
        return $data;
    }
    /**
    *商品分类添加页面
    */
    public function getAdd($id = '')
    {  
        return view('/admin/cate/add',['data'=>self::getCate(),'id' => $id]);
    }
    /**
    *执行商品分类添加
    */
    public function postInsert(Request $request)
    {
        //表单验证
        //validate方法接受http传入的请求以及验证规则，第三个参数是如果验证失败，自定义显示的错误消息
        $this -> validate($request,[
            'cname' => 'required | unique:shop_cate,cname',
        ],[
            'cname.required' => '分类名称为空',
            'cname.unique' => '分类名称不能重复'
        ]);
        $arr = $request -> only(['cname','pid']);
        $res = DB::table('shop_cate')->where('id',$arr['pid'])->first();
        if($arr['pid'] == 0)
            $arr['path'] = 0;
        else 
            $arr['path'] = $res['path'].','.$res['id'];
        //dd($data);
        $res = DB::table('shop_cate')->insert($arr);
        if($res)
            return redirect('/admin/cate/index')->with('success','分类添加成功');
        else
            return redirect('/admin/cate/index')->with('error','分类添加失败');
    }
    /**
    *分类列表
    */
    public function getIndex(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
        $data = DB::table('shop_cate')->where('cname','like','%'.$search.'%')->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB :: table('shop_cate') -> where('cname','like','%'.$search.'%') ->get()); 
        $end = min($page * $count,$sum);
        return view('/admin/cate/index',['data'=>$data,'request'=>$request -> all(), 'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *分类删除页面
    */
    public function getDelete($id)
    {
        $res = DB::table('shop_cate')->where('id',$id)->delete();
        if($res)
            return redirect('/admin/cate/index')->with('success','分类删除成功');
        else
            return redirect('/admin/cate/index')->with('error','分类删除失败');
    }
    /**
    *分类修改页面
    */
    public function getEdit($id)
    {
        $data1 = DB::table('shop_cate')->where('id',$id)->first();
        //判断当前类是否有子类
        $data2 = DB::table('shop_cate') -> where('pid', $id) -> first();
        //dd($data2);
        if($data2){
            return back() -> with('error', '当前类别有子类，不可以修改');
        }
        //dd($data1);
        return view('/admin/cate/edit',['data'=>self::getCate(),'data1'=>$data1]);
    }
    /**
    *分类修改执行页面
    */
    public function postUpdate(Request $request)
    {
        $arr = $request -> only(['cname','pid']);
        $id = $request -> input('id'); 
        $pid = $request -> input('pid'); 
        if($pid == 0){
            $arr['path'] = 0;
        }else{
            $res = DB::table('shop_cate') -> where('id',$pid) -> first();
            $arr['path'] = $res['path'].','.$res['id'];
        }
        $res = DB::table('shop_cate') -> where('id',$request -> input('id')) -> update($arr);
        if($res)
            return redirect('/admin/cate/index')->with('success','分类修改成功');
        else
            return redirect('/admin/cate/index')->with('error','分类修改失败');
    }
}
